% % File		:	verity/ppg_0_1_display
% % Description	:   for Heart Rate Algorithm
% % Fuction		:	PPG signal display
% % Author		:	@hiyangdong
% % Version		:	V0.1
% % Time		:	12 Jan. 2016

%{
 1.0.1 read data and orignal calculate
%}
clear all
orignal_txt = textread('PAH0312_2117.txt','%s');    %#ok<DTXTRD> %read txt file data 32bit with 4byte and Low byte first
orignal_dec = hex2dec(orignal_txt);                 %hex 2 dec every byte
orignal_num = length(orignal_dec);                  %length of the orignal data
ppg_data_byte = 20;
ppg_num  = orignal_num/ppg_data_byte;               %length of the PPG data
ppg_data = zeros(1,ppg_num);                        %generate an 1-by-length empty array

%{
1.0.2 convert orignal hexadecimal data
data format first frame:
1 byte address
2 bytes PPG orignal data
2 bytes Gyro X
2 bytes Gyro Y
2 bytes Gyro Z
2 bytes Acce X
2 bytes Acce Y
2 bytes Acce Z

data format second frame:
1 byte address
2 bytes PPG orignal data
2 bytes Gyro X
2 bytes Gyro Y
2 bytes Gyro Z
2 bytes Magn X
2 bytes Magn Y
2 bytes Magn Z
2 bytes Barometer Z
2 bytes Temprature
%}

j=1;
for i=1:ppg_data_byte:orignal_num
    head_data = orignal_dec(i);
    j=j+1;
end

j=1;
for i=1:ppg_data_byte:orignal_num
    ppg_data(j)=orignal_dec(i+1)*256 + orignal_dec(i+2);
    j=j+1;
end
% ppg_data = fliplr(ppg_data);
% 1.1.2 convert orignal hexadecimal data to ppg 16bit array
j=1;
m = 3;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    gyro_x_data(j) = temp_data;%typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 5;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    gyro_y_data(j) = temp_data;%typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 7;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    gyro_z_data(j) = typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 9;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    acce_x_data(j) = typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 11;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    acce_y_data(j) = typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 13;
for i=1:ppg_data_byte:orignal_num
    temp_data = orignal_dec(i+m)*256 + orignal_dec(i+m+1);
    acce_z_data(j) = typecast (uint16(temp_data),'int16');
    j=j+1;
end

j=1;
m = 15;
for i=1:ppg_data_byte:orignal_num
    magn_x_data(j) = typecast (uint8(orignal_dec(i+m)),'int8');
    j=j+1;
end

j=1;
m = 16;
for i=1:ppg_data_byte:orignal_num
    magn_y_data(j) = typecast (uint8(orignal_dec(i+m)),'int8');
    j=j+1;
end

j=1;
m = 17;
for i=1:ppg_data_byte:orignal_num
    magn_z_data(j) = typecast (uint8(orignal_dec(i+m)),'int8');
    j=j+1;
end

j=1;
m = 18;
for i=1:ppg_data_byte:orignal_num
    baro_data(j) = typecast (uint8(orignal_dec(i+m)),'int8');%orignal_dec(i+m);
    j=j+1;
end

j=1;
m = 16;
for i=1:ppg_data_byte:orignal_num
    temp_data(j) = single((orignal_dec(i+m)+100)/10.0);
    j=j+1;
end

% ppg_data_temp(1)=0;
% ppg_data_int16 = int16(magn_x_data);
% for i=2:length(ppg_data_int16)
%     ppg_data_temp(i) = ppg_data_int16(i)+ppg_data_temp(i-1);
% end
% acce_x_data = ppg_data_temp;
% gyro_z_data = datafilter_success(ppg_data_temp);
% ppg_data=gyro_x_data;
% 
% 
% % magn_y_data = diff(gyro_x_data);
% % magn_y_data(length(gyro_x_data)) = 0;
% 
% magn_z_data = diff(gyro_x_data);
% magn_z_data(length(gyro_x_data)) = 0;
% ppg_data_fliter = gyro_y_data;

%{
1.0.3 ppg data time domain analysis
%}
ppg_num = length(ppg_data);
sampletime = 0.01          %sample time that unit is second
t = 0:sampletime:(ppg_num-1) * sampletime;      %time point

figure(1);
clf();
subplot(2,2,1);
plot(t,ppg_data,'k');
hold on
title('Time domain orignal PPG data');

%{
1.0.4 ppg data frequency domain FFT analysis
%}
samplefreq=1/sampletime;    %sample frequency that unit is Hz
f=samplefreq*(0:(ppg_num-1))/ppg_num;        %frequency point
sampledata_offset=ppg_data-mean(ppg_data);

fft_sampledata=fft(sampledata_offset);
fft_sampledata_abs=abs(fft_sampledata);

N=ppg_num;
subplot(2,2,3)
plot(f,fft_sampledata_abs,'k');
title('Frequency domain orignal PPG data ');

%{
1.0.5 lowpass filter design
%}
fpass = 5
fstop = samplefreq;
f_num = fix(fpass*length(t)/samplefreq);
fft_sampledata(f_num:(length(t)-f_num+1)) = 0;

%{
1.0.6 lowpass filtered data display
%}
ppg_fliterdata = real(ifft(fft_sampledata)) + mean(ppg_data);
subplot(2,2,2);
plot(t,ppg_fliterdata);
hold on
title('Time domain filtered PPG data');

%{
1.0.7 filtered data frequency domain FFT analysis and plot
%}
% fft_fliterdata=fft(ppg_fliterdata);
% fft_fliterdata_abs=abs(fft_fliterdata);

subplot(2,2,4);
fft_fliterdata_abs = abs(fft_sampledata);
plot(f,abs(fft_sampledata),'b');
title('Frequency domain filtered PPG data');

%{
1.0.7 peaks and throughs detection analysis and plot
%}
ds=diff(ppg_fliterdata);
ds=sign(ds);
ds=diff(ds);
throughs=find(ds>0); %when 0 change to 2 the point disappear
peaks=find(ds<0);

subplot(2,2,2);
plot(t(peaks+1),ppg_fliterdata(peaks+1),'or');
plot(t(throughs+1),ppg_fliterdata(throughs+1),'b*');

%{
1.0.8 heart beat calculate
%}
ppg_peak = 0;
ppg_time = max(t)
peaks_num = length(peaks)
ppg_peak = peaks_num;
heart_rate = ppg_peak/ppg_time*60

%{
1.0.9 plot other data
%}

% % 1.0.0  heart beat calculate
figure(2);
plot_data = zeros(6,N);
plot_data(1,:) = gyro_x_data;
plot_data(2,:) = gyro_y_data;
plot_data(3,:) = gyro_z_data;
plot_data(4,:) = acce_x_data;
plot_data(5,:) = acce_y_data;
plot_data(6,:) = acce_z_data;

for i = 1:6
    subplot(2,3,i);
    plot(t,plot_data(i,:),'k');
end

% % 1.0.1  heart beat calculate
figure(3);
plot_data(1,:) = magn_x_data;
plot_data(2,:) = magn_y_data;
plot_data(3,:) = magn_z_data;
plot_data(4,:) = baro_data;

for i = 1:4
    subplot(2,2,i);
    plot(t,plot_data(i,:),'k');
end

